import loginVue from '@/views/login/login.vue'
import notFoundVue from '@/views/not-found/not-found.vue'
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import localCache from '@/utils/catch'
import { firstMenu } from '@/utils/map-menus'
const routes: Array<RouteRecordRaw> = [
  // 重定向到首页
  {
    path: '/',
    redirect: '/main'
  },
  {
    path: '/login',
    name: 'login',
    component: loginVue
  },
  {
    path: '/main',
    name: 'main',
    component: () => import('@/views/main/main.vue')
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'notFound',
    component: notFoundVue
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 路由守卫拦截一下，判断是否需要跳转到login页
router.beforeEach((to) => {
  if (to.path !== '/login') {
    const token = localCache.getCache('token')
    if (!token) {
      return '/login'
    }
  }

  console.log(router.getRoutes())
  console.log(to) // route对象
  // if (to.path.indexOf('/main') !== -1) {
  //   if (to.name === 'notFound') {
  //     to.name = 'user'
  //   }
  // }

  if (to.path === '/main') {
    console.log(firstMenu, 123)
    return firstMenu.url
  }
})

export default router
